package ruifeng.zcj.communcationring.ui.shopping;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.webkit.WebView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.util.Timer;
import java.util.TimerTask;

import ruifeng.zcj.communcationring.base.App;
import ruifeng.zcj.communcationring.base.PayDemoActivity;
import ruifeng.zcj.communcationring.base.api.OSChinaApi;
import ruifeng.zcj.communcationring.base.base_web_activity.other.JS;
import ruifeng.zcj.communcationring.groble.FragmentArgs;
import ruifeng.zcj.communcationring.groble.WeiXinPay;
import ruifeng.zcj.communcationring.ui.circle.other.CircleJS;

/**
 * 商城
 * Created by Administrator on 2016/9/10 0010.
 */
public class ShoppingActivity extends PayDemoActivity {
    public static void launch(Activity activity,
                              Class<? extends Fragment> clazz,
                              FragmentArgs args) {
        Intent intent = new Intent(activity, ShoppingActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getName());
        if (args != null) {
            intent.putExtra(KEY_ARGS, args);
        }
        activity.startActivity(intent);
    }

    @Override
    public void initView() {
        String className = getIntent().getStringExtra(KEY_CLASS_NAME);
        if (TextUtils.isEmpty(className)) {
            finish();
            return;
        }
        FragmentArgs values = (FragmentArgs) getIntent().getSerializableExtra(KEY_ARGS);
        MyURL = (String) values.get(ShoppingFragment.FILM_URL);


        super.initView();
        payOrder(null);
    }


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        mWebview = view;
        return true;
    }

    // 是否调用js代码默认是传递
    @Override
    protected boolean isAddJavascriptInterface() {
        return true;
    }


    // new一个子类
    @Override
    public JS newJsObj() {// 传递一个子类给
        CircleJS aa = new CircleJS();
        aa.jsona = "zcj";
        return aa;
    }

    // 一个key
    @Override
    public String HTMLkey() {// 获取他的key
        return "Android";
    }

    // js代码调用java代码之后该做什么事情做什么事情
    public void pay(String arg) {
        if (!TextUtils.isEmpty(arg)) {
//            order(1);
            payOrder(null);
           // WeiXinPay.WeiXinPay(App.getAppContext(),);
              /*if (arg.indexOf("2") != -1){
                order(2);// 微信支付
            }else {
                order(1);
            }*/
            //  showSnackBar(""+arg);

        }
    }


    public void order(final int i) {

        OSChinaApi.submitorder(i,new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                System.out.println("郑朝军"+responseInfo.result);
                if (i == 1){// 支付包
                   // WeiXinPay.WeiXinPay(App.getAppContext(),);
                }else {
                    payOrder(null);
                }
                showSnackBar("onSuccess 请重新提交");
            }
            @Override
            public void onFailure(HttpException e, String s) {
                showSnackBar("提交订单失败，请重新提交");
            }
        });
    }
}
